package com.li.shop.controller;

import com.li.shop.domain.User;
import com.li.shop.service.VipService;
import com.li.shop.utils.SearchUserResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")

public class VipController {

    //王志文
    @Autowired
    private VipService vipService;
    @RequestMapping(value = "/searchvip", method = RequestMethod.GET)
    public String userPage(Model model , Integer pageSize, Integer pageNum) {
        //TODO: Add user search result
        //普通显示
        // List<user> Users = userService.listUser( pageSize,pageNum);
        //model.addAttribute("users", users);
        //return "user-list-bak";//普通页面
        //return "user-list";//adminLTE页面
        return  "/back/user-listvipbark";
    }
    //分页显示
    @GetMapping("/api/searchvip")
    @ResponseBody//需要返回json数据
    public SearchUserResponse searchUser(Integer pageNum, Integer pageSize , String keyword){
        return  vipService.listUser(pageSize,pageNum,keyword);
    }

    @RequestMapping(value = "/addvip", method = RequestMethod.GET)
    public String addUserPage(Model model) {
        model.addAttribute("user", new User());
        return "/back/user-addvip";
    }

    @GetMapping("/editvip")
    public String editUserPage(@RequestParam("id") String id, Model model) {
        User user = vipService.findUserByPrimaryKey(id);
        model.addAttribute("user", user);
        return "/back/user-addvip";
    }

    @RequestMapping(value = "/addvip", method = RequestMethod.POST)
    public String saveUser(User user) {
        int updated = vipService.saveUser(user);
        return "redirect:/user/searchvip";
    }
    @RequestMapping (value = "/deletevip",method=RequestMethod.GET)
    //@ResponseBody//需要返回json数据 @RequestParam("id") @PathVariable("id")
    public String deleteUser(@RequestParam("id") String id) {
        //var mymessage = confirm("确认删除嘛？");
        //if (mymessage == true) {
        int deleted = vipService.deleteUserByPrimaryKey(id);

        // return deleted > 0;
        //return "redirect:/backadminuser/searchuser";//get请求不能用重定向
        return "/back/user-listvipbark";
        //return "redirect:/backadminuser/deleteuser";
    }
}
